$arr = array(
---------------------------
array(
'name' => '蘋果',
'count' => 1,
),
array(
'name' => '蘋果',
'count' => 2,
),
array(
'name' => '蘋果',
'count' => 3,
),
---------------------------
array(
'name' => '橘子',
'count' => 1,
),
---------------------------
);
$result = array();
foreach($arr as $val){
$key = $val['name']; //填入要塞選的項目
if(!isset($result[$key])){
$result[$key] = $val;
}else{
$result[$key]['count'] += $val['count']; //填入要疊加的項目
}
}
print_r($result);
輸出:
Array
(
[蘋果] => Array
(
[name] => 蘋果
[count] => 6
)
[橘子] => Array
(
[name] => 橘子
[count] => 1
)
)
$block_name = explode(",",$v['block_name']);
$new_block = array();
// echo $v['block_name'];
if(in_array(1,$block_name)){
$new_block[] = "上午場";
}
if(in_array(2,$block_name)){
// echo ",下午場";
$new_block[] = "下午場";
}
echo implode(',',$new_block);